#include <ctime>
#include <cstdlib>
#include "igslib_iburam.h"
namespace igslib
{
    namespace buram_sample
    {
        struct BuRAM_Stuff
        {
            int a;
            float b;
            char c;
            char d[64];
            BuRAM_Stuff()
            {
                time_t t;
                a = 1;
                b = 2.3f;
                c = 'a';
                time(&t);
                strcpy(d,ctime(&t));
            }
            void change()
            {
                time_t t;
                ++a;++b;++c;
                time(&t);
                strcpy(d,ctime(&t));
            }
        };
        /*!
         * 此範例展示BuRAM如何存取非揮發性記憶體。
         * 每次執行後，都會顯示目前BuRAM_Stuff內的值，而且都會變更一次。
         */
        static void Simplest()
        {
            const int show_length = 20;
            IBuRAMManager* brm = GetBuRAMManagerInstance();
            IBuRAM* br = brm->NewM01BuRAM();

            void* block = br->BindBlock(br->GetSize());
            BuRAM_Stuff* brs = (BuRAM_Stuff*)block;
            util::Log("%s> block=%p,brs=%p,size=%d\n",__func__,block,brs,br->GetSize());

            {
                unsigned char* buf = (unsigned char*)block;
                printf("%s> last:",__func__);
                for ( int i = 0 ; i < show_length ; ++i )
                {
                    printf("%02x ",*buf++);
                }
                printf("\n");
            }
            
            brs->change();

            {
                unsigned char* buf = (unsigned char*)block;
                printf("%s> change:",__func__);
                for ( int i = 0 ; i < show_length ; ++i )
                {
                    printf("%02x ",*buf++);
                }
                printf("\n");
            }

            exit(EXIT_SUCCESS);
        }
    };
};

